{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<form
  {{on
    "submit"
    (fn
      @doSubmit
      (hash param=(compact (array @exportKeyType (if this.exportVersion @exportKeyVersion))))
      (hash wrapTTL=@wrappedTTL)
    )
  }}
  ...attributes
>
  <div class="box is-sideless is-fullwidth is-marginless">
    <div class="content">
      <p>Export a key using <code>{{@key.name}}</code> as the cryptographic key.</p>
    </div>
    <div class="field">
      <label for="type" class="is-label">Key type</label>
      <div class="control is-expanded">
        <div class="select is-fullwidth">
          <select name="type" id="type" onchange={{action (mut @exportKeyType) value="target.value"}}>
            {{#each @key.exportKeyTypes as |currOption|}}
              <option selected={{eq @exportKeyType currOption}} value={{currOption}}>
                <code>{{currOption}}</code>
              </option>
            {{/each}}
          </select>
        </div>
      </div>
    </div>
    <div class="field">
      <div class="b-checkbox">
        <Input @type="checkbox" name="exportVersion" id="exportVersion" class="styled" @checked={{this.exportVersion}} />
        <label for="exportVersion" class="is-label">
          Export a single version
        </label>
      </div>
      {{#if this.exportVersion}}
        <div class="field">
          <label for="version" class="is-label">Version</label>
          <div class="control is-expanded">
            <div class="select is-fullwidth">
              <select name="version" id="version" onchange={{action (mut @exportKeyVersion) value="target.value"}}>
                {{#each @key.validKeyVersions as |versionOption|}}
                  <option selected={{eq @exportKeyVersion versionOption}} value={{versionOption}}>
                    <code>{{versionOption}}</code>
                    {{#if (eq @key.validKeyVersions.lastObject versionOption)}}
                      <span> (latest) </span>
                    {{/if}}
                  </option>
                {{/each}}
              </select>
            </div>
          </div>
        </div>
      {{/if}}
    </div>
    <WrapTtl @onChange={{fn (mut @wrappedTTL)}} />
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Button @text="Export key" type="submit" />
    </div>
  </div>
</form>
{{#if @isModalActive}}
  <Hds::Modal id="transit-export-modal" @onClose={{fn (mut @isModalActive) false}} as |M|>
    <M.Header>
      Copy your wrapped key
    </M.Header>
    <M.Body>
      <h2 class="title is-6">Wrapped key</h2>
      {{#if @wrappedTTL}}
        <Hds::Copy::Snippet
          class="has-bottom-margin-m"
          @textToCopy={{@wrappedToken}}
          @color="secondary"
          @container="#transit-export-modal"
          @isFullWidth={{true}}
          @isTruncated={{true}}
          @onError={{(fn
            (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
          )}}
          data-test-encrypted-value="export"
        />
      {{else}}
        <div class="copy-text level is-relative">
          <pre data-test-encrypted-value="export" class="level-left">
            {{stringify @keys}}
          </pre>
          <Hds::Copy::Button
            @text="Copy"
            @isIconOnly={{true}}
            @textToCopy={{stringify @keys}}
            @container="#transit-export-modal"
            @onError={{(fn
              (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
            )}}
            class="transparent top-right-absolute"
          />
        </div>
      {{/if}}
    </M.Body>
    <M.Footer as |F|>
      <Hds::Button @text="Close" {{on "click" F.close}} />
    </M.Footer>
  </Hds::Modal>
{{/if}}